ifndef APP
APP := $(notdir $(abspath $(lastword $(MAKEFILE_LIST)/..)))
endif

.PHONY: $(APP) sysflow cvtable mpxcopy

LMKDIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
TOPDIR := $(abspath $(LMKDIR)/../../..)
FMWDIR := $(TOPDIR)/arm_framework
MKNAME := $(notdir $(lastword $(MAKEFILE_LIST)))

ifndef CMNDIR
CMNDIR = $(TOPDIR)/cv_common
endif

ifndef BUILDDIR
BUILDDIR = $(TOPDIR)/build
endif

include $(CMNDIR)/build/cvtask_lib.inc

TOP_BINDIR = $(BUILDDIR)/bin

OBJDIR = $(BUILDDIR)/output/app/$(APP)
BINDIR = $(TOP_BINDIR)/app/$(APP)

APP_SRCS := $(wildcard $(LMKDIR)/*.c)
APP_SRCS += $(wildcard $(LMKDIR)/*.cpp)
APP_SRCS += $(wildcard $(LMKDIR)/src/*.cpp)
APP_SRCS += $(wildcard $(LMKDIR)/src/common/*.cpp)
APP_SRCS += $(wildcard $(LMKDIR)/src/preprocess/*.cpp)
# APP_SRCS += $(wildcard $(LMKDIR)/src/postprocess/*.cpp)


# APP_SRCS := $(LMKDIR)/*.c
# APP_SRCS += $(LMKDIR)/*.cpp
all: app sysflow mpxcopy cvtable

include $(CMNDIR)/build/cvtask_lib.inc
include $(CMNDIR)/build/cvtable.inc
include $(CMNDIR)/build/sysflow.inc
include $(CMNDIR)/build/mpxcopy.inc
include $(CMNDIR)/build/linker_script.inc

CFLAGS := -I$(CMNDIR)/inc -I$(CMNDIR)/inc/cvapi -D$(CVCHIP) -I$(TOPDIR)/../ipcdef -I$(STAGING_DIR)/usr/include/opencv4  \
			-I$(STAGING_DIR)/usr/include/ -I$(STAGING_DIR)/usr/include/eigen3 -I$(STAGING_DIR)/usr/include/boost \
			-I$(LMKDIR)/include  -I$(LMKDIR)/include/common  -I$(LMKDIR)/include/preprocess
			# -I$(LMKDIR)/include/postprocess

LFLAGS := -L$(TOP_BINDIR)/lib -L$(PREBUILDDIR)
LFLAGS += -pthread -lambadag -lm -lrt -lpthread -lstdc++  \
			-lopencv_core -lopencv_imgcodecs -lopencv_imgproc -lboost_filesystem -lboost_json

# CFLAGS := -I$(CMNDIR)/inc -I$(CMNDIR)/inc/cvapi -D$(CVCHIP) -I$(TOPDIR)/../ipcdef \
# 			-I$(STAGING_DIR)/usr/include/ -I$(STAGING_DIR)/usr/include/eigen3
# LFLAGS := -L$(TOP_BINDIR)/lib -L$(PREBUILDDIR)			
# LFLAGS += -pthread -lambadag -lm -lrt -lpthread -lstdc++

ifdef CV_COMMON_LFLAGS
LFLAGS += $(CV_COMMON_LFLAGS)
endif
ifeq ($(ENABLE_CVTASK_CAMCTRL_IPC),y)
LFLAGS += -lambaipc -laipc_cfg -lambatransfer
endif
LFLAGS += -lambaflexidagio
LFLAGS += -lambamal

LFLAGS += -Xlinker -Map=$(OBJDIR)/$(APP).map

#APP_OBJS   = $(subst $(LMKDIR),$(OBJDIR),$(APP_SRCS:.c=.o),$(APP_SRCS:.cpp=.o))

ifneq ("$(wildcard $(TOP_BINDIR)/lib/libambadag.a)","")
SCHEDULER_LIB = $(TOP_BINDIR)/lib/libambadag.a
endif

#CFLAGS += -Wall -Werror  -std=c++14  -U__FILE__ -D__FILE__='"$(subst $(dir $<),,$<)"'
CFLAGS += -g -rdynamic
LFLAGS += -g -rdynamic

#$(LFLAGS) has to put after $(CVTASK_LIBS_FLAGS) $(CC)
app: $(BINDIR)/$(APP)
$(BINDIR)/$(APP): $(APP_OBJS) $(APP_LSR) $(SCVTASK_LIBS) $(SCVLIB_LIBS) $(SCHEDULER_LIB)
	@echo "        Building App: $@"
	@mkdir -p $(dir $@)
	$(CC) $(APP_SRCS) -o $@ -T $(APP_LSR)  $(CVTASK_LIBS_FLAGS) $(LFLAGS) $(SCHEDULER_LIB) $(CFLAGS)
ifeq ("$(STATIC_LINK)","n")
ifneq ("$(LCVTASK_LIBS) $(LCVLIB_LIBS)"," ")
	install -m 755 -D -t $(BUILDDIR)/bin/lib/prebuild $(LCVTASK_LIBS) $(LCVLIB_LIBS)	
endif
endif

# -include $(APP_OBJS:.o=.d)

# $(OBJDIR)/%.o: $(LMKDIR)/%.c
# 	@echo "        Compiling:    $<"
# 	@mkdir -p $(dir $@)
# 	$(CC) -c $(CFLAGS) -o $@ $<
# 	@echo -n $(dir $@) > $(OBJDIR)/$*.d
# 	@$(CC) -MM $(CFLAGS) $< >> $(OBJDIR)/$*.d

